const mysql  = require('mysql2/promise');
const {connection} = require('./common');

async function check(para){
    const admin = para;
    let count = 0;
    let sql = 'SELECT * FROM admin';
    let res = await connection.execute(sql);
    let array = [];
    let record;
    let turn;
    for(var i in res[0]){
      let obj = {
        adminName:res[0][i].adminName,
        passwd:res[0][i].passwd
      }
      array.push(obj)
    }
    for(var i in res[0]){
      if(res[0][i].adminName == admin.username && res[0][i].passwd == admin.password){
        count++;
        record = i;
      }
      else{
        count+=0;
      }
    }
    if(count){
      turn = {
        status:'success',
        data:{
          username:res[0][record].adminName,
          adminImg:res[0][record].adminImg
        }
      }
    }else{
      turn = {status:'fail'}
    }
  
    return JSON.stringify(turn);
}

module.exports = {
    check
}